package com.fjnu.controller.shiro;

import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.Permission;
import com.fjnu.entities.Role;
import com.fjnu.service.IPermissionService;
import com.fjnu.service.IRoleService;
/**
 * 角色和权限之间的相关操作
 * @author 默默
 *
 */
@Controller
public class RolePermissionController extends BaseController{
	private static Logger logger =LogManager.getLogger(RolePermissionController.class);
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IPermissionService permissionService;
    @ResponseBody
    @RequestMapping(value="/addPermissionToRole")
	public Map<String,Object> addPermissionToRole(int roleId,int permissionId){
    	logger.info("给角色加权限");
		if(!init(roleId,permissionId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Role role = roleService.findRoleUnique(roleId);
		Permission permission = permissionService.findPermissionUnique(permissionId);
		role.getPermissionList().add(permission);
		permission.getRole().add(role);
		permissionService.addPermission(permission);
		roleService.addRole(role);
		return getData2Result();
	}
    @ResponseBody
    @RequestMapping(value="/delPermissionToRole")
    public Map<String,Object> delPermissionToRole(int roleId,int permissionId){
    	logger.info("删除角色的权限");
    	if(!init(roleId,permissionId).get(STATUS).equals(SUCCESS))
			return getData2Result();
    	Role role = roleService.findRoleUnique(roleId);
		Permission permission = permissionService.findPermissionUnique(permissionId);
		role.getPermissionList().remove(permission);
		permission.getRole().remove(role);
		permissionService.addPermission(permission);
		roleService.addRole(role);
    	return getData2Result();
    }
}
